
// Things are a bit oddly named right now. Fix later
#include "run_proxy.hh"
#include "Socket.hh"

#include <cstdlib>
// #include <climits>
#include <iostream>
#include <stdexcept>
using namespace std;

static const int backlog = 5;

int run_proxy( unsigned port ) {
    try {
        InetAddress addr( INADDR_ANY, port );
        Socket socket( AF_INET, SOCK_STREAM, 0 );
        if ( -1 == socket.set_nonblock() ) {
            throw std::runtime_error("set_nonblock()");
        }
        if ( -1 == socket.bind( addr ) ) {
            throw std::runtime_error("bind()");
        }
        if ( -1 == socket.listen( backlog ) ) {
            throw std::runtime_error("listen()");
        }
    } catch ( std::exception const & e ) {
        cerr << e.what() << endl;
        return -1;
    }
    
    return 0;
}
